/** @odoo-module **/

import { Component } from "@odoo/owl";
import { FormController } from "@web/views/form/form_controller";
import { patch } from "@web/core/utils/patch";

// 扩展FormController，添加Chatter切换功能
patch(FormController.prototype, {
    setup() {
        super.setup(...arguments);
        this.chatterVisible = true;
    },
    
    async onMounted() {
        await super.onMounted(...arguments);
        this.addChatterToggleButton();
    },
    
    addChatterToggleButton() {
        // 等待DOM加载完成
        setTimeout(() => {
            const chatterEl = document.querySelector('.o-mail-Chatter');
            const topbarEl = document.querySelector('.o-mail-Chatter-topbar');
            
            if (chatterEl && topbarEl && !topbarEl.querySelector('.chatter-toggle-btn')) {
                // 创建收放按钮
                const toggleBtn = document.createElement('button');
                toggleBtn.className = 'chatter-toggle-btn';
                toggleBtn.innerHTML = '◀';
                toggleBtn.title = '收起/展开右侧边栏';
                
                // 添加点击事件
                toggleBtn.addEventListener('click', () => {
                    this.toggleChatter();
                });
                
                // 添加到topbar
                topbarEl.appendChild(toggleBtn);
            }
        }, 1000);
    },
    
    toggleChatter() {
        const chatterEl = document.querySelector('.o-mail-Chatter');
        const formSheet = document.querySelector('.o_form_sheet_bg');
        const toggleBtn = document.querySelector('.chatter-toggle-btn');
        
        if (chatterEl && toggleBtn) {
            if (this.chatterVisible) {
                // 隐藏Chatter
                chatterEl.style.width = '0';
                chatterEl.style.minWidth = '0';
                chatterEl.style.overflow = 'hidden';
                chatterEl.style.padding = '0';
                chatterEl.style.border = 'none';
                
                if (formSheet) {
                    formSheet.style.maxWidth = '100%';
                }
                
                toggleBtn.innerHTML = '▶';
                toggleBtn.classList.add('collapsed');
                this.chatterVisible = false;
                
                // 保存状态到localStorage
                localStorage.setItem('chatter_collapsed', 'true');
            } else {
                // 显示Chatter
                chatterEl.style.width = '';
                chatterEl.style.minWidth = '';
                chatterEl.style.overflow = '';
                chatterEl.style.padding = '';
                chatterEl.style.border = '';
                
                if (formSheet) {
                    formSheet.style.maxWidth = '';
                }
                
                toggleBtn.innerHTML = '◀';
                toggleBtn.classList.remove('collapsed');
                this.chatterVisible = true;
                
                // 保存状态到localStorage
                localStorage.setItem('chatter_collapsed', 'false');
            }
        }
    },
    
    async onWillStart() {
        await super.onWillStart(...arguments);
        
        // 检查localStorage中的状态
        const collapsed = localStorage.getItem('chatter_collapsed') === 'true';
        if (collapsed) {
            this.chatterVisible = false;
        }
    }
});

